<?php

namespace app\admin\services;

use app\auth\service\BaseService;
use app\common\models\BannerModel;

class BannerService extends BaseService
{

    protected $model;
    public function __construct()
    {
        $this->model = new BannerModel();
    }
    /**
     * 获取数据
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getData($params) {
        $condition = $this->getCondition($params);
        $count = $this->model->where($condition)->count();
        $data = $this->model->where($condition)->order('id desc')
            ->page($params['page'],$params['limit'])->select();
        return ['code' => $this->successCode,'msg' => $this->successMsg,'data' => $data,'count' => $count];
    }

    public function add($data)
    {
        $params = [
            'title' => $data['title'],
            'type' => $data['type'],
            'sort' => $data['sort'],
        ];
        if ($data['type'] == 1) {
            $params['file_src'] = $data['img_src'];
        }else {
            $params['file_src'] = $data['videoSrc'];
        }
        return parent::add($params);
    }

    public function modify($where ,$data) {
        $params = [
            'id' => $data['id'],
            'title' => $data['title'],
            'type' => $data['type'],
            'sort' => $data['sort'],
        ];
        if ($data['type'] == 1) {
            $params['file_src'] = $data['img_src'];
        }else {
            $params['file_src'] = $data['videoSrc'];
        }
        return parent::modify($where,$params);
    }

    private function getCondition($params) {
        $condition = $this->initCondition();
        return $condition;
    }
}